﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KCL.Code.Business;
using KCL.Code.Entities;
using KCL.Code.Data;

namespace KCL.Management
{
	public partial class OrderEdit : System.Web.UI.Page
    {
		public Order currentOrder;
        public Customer currentCustomer;
        public string selectMaterial, phaseList, divSelectPhases, strCurrentOrder;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session[SessionManager.InSection] != null)
			{
				if ((int)Session[SessionManager.InSection] != 1)
				{
					Response.Redirect("../");
				}
			}
			else
			{
				Response.Redirect("../");
			}
			OrderDAO orderDAO = new OrderDAO();
            CustomerDAO customerDAO = new CustomerDAO();
            PhaseDAO phaseDAO = new PhaseDAO();
            List<Code.Material> materials = MaterialServices.GetAllMaterials();
			currentOrder = orderDAO.GetOrderDetail(Convert.ToInt32(Request.Params["id"]));
            strCurrentOrder = Newtonsoft.Json.JsonConvert.SerializeObject(currentOrder);
            currentCustomer = customerDAO.GetCustomer(currentOrder.customerId);
            List<Phase> phases = phaseDAO.GetAllPhases();
            selectMaterial = "<select id=\\\"comboMaterials\\\">";
            foreach (KCL.Code.Material material in materials)
                selectMaterial += string.Format("<option value=\\\"{0}\\\">{1}</option>", material.code, material.name);
            selectMaterial += "</select>";

            phaseList = "var phases = [";
            divSelectPhases = "<div id='divSelectPhase' style='text-align: center'><table style='text-align: right; border: 0px; border-spacing: 4px'><tr><td><b>Tên công đoạn</b></td><td><b>Giá</b></td><td><b>KL</b></td><td><b>SL</b></td></tr>";
            int index = 0;
            foreach (Phase phase in phases)
            {
                phaseList += string.Format("{{\"id\":{0},\"name\":\"{1}\",\"price\":{2}}},", phase.id, phase.name, phase.price);
                divSelectPhases += string.Format("<tr><td>{0}</td><td>{1}</td><td><input type='text' value='' id='phaseWeightmaterialIndex{2}' size='4'/></td><td><input type='text' value='0' id='phaseQuantitymaterialIndex{2}' size='2'/></td></tr>", phase.name, phase.price, phase.id);
                index++;
            }
            phaseList = phaseList.Remove(phaseList.Length - 1, 1) + "];";
            divSelectPhases += "</table><button onclick='RefreshPrice(materialIndex); $(\"#divSelectPhase\").dialog(\"close\"); return false;' class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary'><img src='../styles/images/phase.png' alt='Chọn' style='border:0; width:20px; padding: 5px 0px 2px 7px' /><span class='ui-button-text' style='float: right; padding-left: 0.7em'>Chọn</span></button></div>";
		}
    }
}